package com.chuanyang.driverh.mainmy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.OnCustomListener;
import com.chuanyang.driverh.responseresult.BankCardData;
import com.chuanyang.driverh.utils.widget.SwipeLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 相思无用，唯别而已。别期若有定，千般煎熬又何如，莫道黯然销魂，何处柳暗花明。
 * Created by sky on 2016/8/12.
 */
public class BankCardAdapter extends BaseAdapter {

    private List<BankCardData> bankCardEntityList;
    private LayoutInflater mInflater;
    private Context mContext;
    private ArrayList<SwipeLayout> openedItems = new ArrayList<SwipeLayout>();
    private OnCustomListener listener;
    ViewHolder holder = null;


    public BankCardAdapter(Context activity, List<BankCardData> orderWaitConfirmList) {
        bankCardEntityList = orderWaitConfirmList;
        mContext = activity;
        mInflater = LayoutInflater.from(activity);
    }
   public  void setList(List bankCardEntityList)
   {
       this.bankCardEntityList=bankCardEntityList;
       notifyDataSetChanged();
   }
    @Override
    public int getCount() {
        return bankCardEntityList.size();
    }

    @Override
    public Object getItem(int position) {
        return bankCardEntityList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.my_item_bank_card, null);
            holder = new ViewHolder();
            holder.iv_bank_card_icon = (ImageView) convertView.findViewById(R.id.iv_bank_card_icon);
            holder.tv_bank_card_name = (TextView) convertView.findViewById(R.id.tv_bank_card_name);
            holder.tv_bank_card_type = (TextView) convertView.findViewById(R.id.tv_bank_card_type);
            holder.tv_bank_card_num = (TextView) convertView.findViewById(R.id.tv_bank_card_num);
            holder.tv_delete= (TextView) convertView.findViewById(R.id.tv_delete);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //holder.tv_goods_name_modification.setText(goodsEntities.get(position).getGoodsName());
        //holder.iv_bank_card_icon.setImageResource(R.mipmap.ic_launcher);
        holder.tv_bank_card_name.setText("中国工商银行");
        holder.tv_bank_card_num.setText("6222******4568");
        holder.tv_bank_card_type.setText("储蓄卡");
        SwipeLayout sl = (SwipeLayout) convertView.findViewById(R.id.sl);
        sl.setOnSwipeListener(new MySwipeListener());
        holder.tv_delete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onCustomerListener(holder.tv_delete, position);
                }

            }
        });
        return convertView;
    }
    public void closeAllItems() {
        // 关闭当前所有打开的条目
        for (int i = 0; i < openedItems.size(); i++) {
            openedItems.get(i).close();
        }

        // 集合清除
        openedItems.clear();
    }

    class MySwipeListener implements SwipeLayout.OnSwipeListener {

        @Override
        public void onOpen(SwipeLayout layout) {
            // 记录当前被开打的条目
            openedItems.add(layout);
        }

        @Override
        public void onClose(SwipeLayout layout) {
            // 关闭了就从集合中移除
            openedItems.remove(layout);
        }

        @Override
        public void onStartOpen(SwipeLayout layout) {
            // 新的条目将要打开
            closeAllItems();
        }

        @Override
        public void onStartClose(SwipeLayout layout) {

        }
    }
    /**
     * 设置适配器上，某个控件的监听事件
     *
     * @param listener
     */
    public void setOnCustomListener(OnCustomListener listener) {
        this.listener = listener;
    }
    public class ViewHolder {
        public ImageView iv_bank_card_icon;
        public TextView tv_bank_card_name;
        public TextView tv_bank_card_type;
        public TextView tv_bank_card_num;
        public TextView tv_delete;
    }
}
